package com.lebang.service;

import com.alibaba.fastjson.JSON;
import com.lebang.domain.ClickStrategyDetail;
import com.lebang.domain.MemberClickRouse;
import com.lebang.example.ClickStrategyDetailExample;
import com.lebang.model.Whether;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 签到策略配置
 *
 * @Author hwq
 * @Date 2020-02-25
 */
@Service
public class ClickStrategyDetailService extends BaseService<ClickStrategyDetail, Long, ClickStrategyDetailExample> {
    @Autowired
    private RedisCacheService redisCacheService;
    private static final String KEY = "ClickStrategyDetail:";

    /**
     * 获取全部数据
     *
     * @return
     */
    public List<ClickStrategyDetail> findAllList(Long id) {
        List<ClickStrategyDetail> appEatConfigs = redisCacheService.selectListByKey(KEY + id, ClickStrategyDetail.class);
        if (appEatConfigs == null) {
            ClickStrategyDetailExample example = new ClickStrategyDetailExample();
            example.setOrderByClause("day_count asc");

            example.createCriteria()
                    .andHasDeletedEqualTo(Whether.NO.getDbIndex())
                    .andEnableEqualTo(Whether.YES.getDbIndex())
                    .andStrategyIdEqualTo(id.intValue());
            appEatConfigs = this.selectByExample(example);
            if (appEatConfigs != null) {
                redisCacheService.addCacheListByKey(KEY + id, JSON.toJSONString(appEatConfigs));
            }
        }
        return appEatConfigs;
    }

    public ClickStrategyDetail selectByDays(int days, MemberClickRouse rouses) {
        List<ClickStrategyDetail> appEatConfigs = this.findAllList(rouses.getStrategyId().longValue());
        for (ClickStrategyDetail next : appEatConfigs) {
            if (days == next.getDayCount() && rouses.getRouseId().equals(next.getRouseId())) {
                return next;
            }
        }
        return null;
    }

    public ClickStrategyDetail selectByDaysLess(int days, MemberClickRouse rouses) {
        List<ClickStrategyDetail> appEatConfigs = this.findAllList(rouses.getStrategyId().longValue());
        for (ClickStrategyDetail next : appEatConfigs) {
            if (next.getDayCount() >= days && rouses.getRouseId().equals(next.getRouseId())) {
                return next;
            }
        }
        return null;
    }
}
